layui.use(['form','layer','table','laytpl','upload'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table,
		upload = layui.upload;
  
    
	
	
	
	form.on("submit(addShop)",function(data){
        //弹出loading
        //alert($("#file").val());
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        var action 	= data.form.action;//表单提交URL地址
        //var da		= data.field;
        console.log(data.field);
        $.post(action,data.field,function(obj){
        	
        });
        // 实际使用时的提交信息
        /*$.post(action,{
         	"shop_name"			: $(".shop_name").val,
         	"shop_wangwang"		: $(".shop_wangwang").val,
         	"shop_type"			: $(".shop_type").val,
         	"user_name"			: localStorage.login_name,
         	"shop_img_url"		: img_url
        },function(res){
        	
        })*/
        setTimeout(function(){
            top.layer.close(index);
            top.layer.msg("商铺添加成功！");
            layer.closeAll("iframe");
            //刷新父页面
            parent.location.reload();
        },2000);
        return false;
    })
	
	
    var tableIns = table.render({
        elem: '#foodmenuList',
        url : 'http://127.0.0.1:88/index.php/font/Font/renwuList?id='+localStorage.login_font_name,
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 20,
        id : "renwuListTable",
		totalRow: true,
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field:'renwu_id', title:'ID', width:70, fixed: 'left', unresize: true, sort: true, totalRowText: '合计'}
            ,{field: 'renwu_good_shop', title: '所属店铺',width:80, align:"center"}
            ,{field: 'renwu_good_searchWord', title: '搜索词', align:'center'}
			,{field: 'renwu_good_price', title: '任务金额', width:60, align:'center'}
            ,{field: 'renwu_good_money', title: '佣金', width:60, align:'center'}
			,{title: '审核状态', templet:'#weishenhe',width:90, align:"center"}
			,{title: '提交截图', templet:'#tijiaocailiao',width:130, align:"center"}
			,{field: 'renwu_isDo', title: '提交', align:'center',templet:function(d){
            	if(d.renwu_isDo==0){
            		return '<button class="layui-btn layui-btn-sm layui-btn-danger" id="Submit" lay-event="Submit">去提交</button>'+'<button class="layui-btn layui-btn-sm layui-btn-danger" id="font_del" lay-event="font_del">删除任务</button>';
            	}else{
					return '<button class="layui-btn layui-btn-sm layui-btn-disabled">已提交</button>';
            	}
            	
                
            }}
			,{field: 'renwu_addTime', title: '添加时间',width:100, align:'center'}
        ]]
    });
	
	
	var yiwancheng = table.render({
        elem: '#yiwancheng',
        url : 'http://127.0.0.1:88/index.php/font/Font/renwuyiwancheng?id='+localStorage.login_font_name,
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 20,
        id : "renwuListTable1",
		totalRow: true,
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field:'renwu_id', title:'ID', width:70, fixed: 'left', unresize: true, sort: true, totalRowText: '合计'}
            ,{field: 'renwu_good_shop', title: '所属店铺',width:80, align:"center"}
            ,{field: 'renwu_good_searchWord', title: '搜索词', align:'center'}
			,{field: 'renwu_good_price', title: '任务金额', width:60, align:'center'}
            ,{field: 'renwu_good_money', title: '佣金', width:60, align:'center'}
			,{title: '审核状态', templet:'#chakanshenhe',width:90, align:"center"}
			,{field: 'renwu_addTime', title: '添加时间',width:100, align:'center'}
        ]]
    });
	$("#smb").click(function() {
		
		layer.msg('----------------');
	})
    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
    	var type = $(this).data('type');
    	//console.log(type);
        if($(".searchVal").val() != ''){
            table.reload("renwuListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                method:'post',
                url:'http://127.0.0.1:88/index.php/renwu/renwu/renwuSearch',
                where: {
                    key: $(".searchVal").val(),  //搜索的关键字
                    user_name:localStorage.login_name
                },
                contentType: 'application/json',
                done: function(res, curr, count){
                  //this.where={};
                  //不清空，有问题：搜索条件会保留
                  //清空，  有问题：分页的时候没有搜索条件了,分页会受到影响
              }
            })
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

   
	//刷新renwu_refresh
	$(".renwu_refresh").click(function(){
        tableIns.reload();
    })
	

    //列表操作
    table.on('tool(foodmenuList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
		id = data.renwu_id;
		
        if(layEvent === 'Submit'){ //编辑
			
			dingdan_jietu="";
			renwu_good_price="";
			msg1="";
			code="";
			$.ajax({ //结束时间
				type: 'POST',
				async:false,
				url: "http://127.0.0.1:88/index.php/font/Font/getdingdanjietu",
				dataType: "json",
				data: "renwu_id="+id+"&user_name="+localStorage.login_font_name,
				success: function(sss) {
					console.log(sss);
					code=sss['code'];
					dingdan_jietu=sss['dingdan_jietu'];
					renwu_good_price=sss['renwu_good_price'];
					msg1=sss["msg"];
				}
			});
			if(code==200){
				var anniu=[ '确认','取消'];
				var kaichuang=layer.open({
					type: 1,
					closeBtn: 0,
					content: '<fieldset class="layui-elem-field site-demo-button">'+'<legend><big><b id="information">订单审核信息</b></big></legend>'+
					'<form class="layui-form layui-form-pane" action="">'+
					'<div id="wangyin" class="layui-fluid layui-form layui-form-pane">'+
						'<div class="layui-form-item">'+
						'<label class="layui-form-label">任务编号</label>'+
						'<div class="layui-input-inline">'+
							'<input class="layui-input" id="renwu_bianhao" type="text" value="'+id+'" readonly="true"/>'+
						'</div>'+
						'</div>'+
						'<div class="layui-form-item">'+
						'<label class="layui-form-label">用户名</label>'+
						'<div class="layui-input-inline">'+
							'<input class="layui-input" id="user_name" type="text" value="'+localStorage.login_font_name+'" readonly="true"/>'+
						'</div>'+
						'</div>'+
						
						'<div class="layui-form-item">'+
						'<label class="layui-form-label">淘宝订单号</label>'+
						'<div class="layui-input-inline">'+
							'<input class="layui-input" id="dingdanhao" type="text" />'+
						'</div>'+
						'</div>'+
						
						'<div class="layui-form-item">'+
						'<label class="layui-form-label">订单金额</label>'+
						'<div class="layui-input-inline">'+
							'<input class="layui-input" id="renwu_good_price" type="text" value="'+renwu_good_price+'"/>'+
						'</div>'+
						'</div>'+
						
						'<div class="layui-form-item">'+
						'<label class="layui-form-label">实付金额</label>'+
						'<div class="layui-input-inline">'+
							'<input class="layui-input" id="shifu_jine" type="text"/>'+
						'</div>'+
						'</div>'+
						'<div class="layui-form-item">'+
						'<div class="layui-upload-list">'+
							'<img class="layui-upload-img" id="demo1" src="'+dingdan_jietu+'" height="300" width="300" >'+
							'<p id="demoText"></p>'+
						'</div>'+
						'</div>'+
					'</div></form></fieldset>',
					btn:anniu,
					yes: function() {
					
						
					$.ajax({ //结束时间
						type: 'POST',
						url: "http://127.0.0.1:88/index.php/font/Font/tijiaocailiao",
						dataType: "json",
						data: "renwu_id="+$("#renwu_bianhao").val()+"&user_name="+$("#user_name").val()+"&renwu_good_price="+$("#renwu_good_price").val()+"&shifu_jine="+$("#shifu_jine").val()+"&dingdan_jietu="+$("#dingdan_jietu").val()+"&dingdanhao="+$("#dingdanhao").val(),
						success: function(sss) {
							layer.msg(sss['msg']);
							
						}
					});
					
					$("#Submit").attr("disabled", true); //存储按钮禁用
					$("#Submit").html("已提交");
					$("#Submit").attr("class", 'layui-btn layui-btn-sm layui-btn-disabled'); //渲染
					$("#font_del").remove();
					$.ajax({ //结束时间
						type: 'POST',
						url: "http://127.0.0.1:88/index.php/font/Font/Submit",
						dataType: "text",
						data: "id="+id,
						success: function(sss) {
							layer.msg(sss);
						}
					});
					
					
					layer.close(kaichuang);
					
					return false;
					},
					btn2: function() {
						
						$("#password").val('');
						layer.close(kaichuang);
						
						return true;
					}
				});
			}
			else{
				layer.msg(msg1);
				if(code==507){
					setTimeout(function(){
						window.location.href = "http://127.0.0.1:89/font/user/tixian.html";
					},2000);
				}
			}
			
        }
		
		else if(layEvent === 'font_del'){ //删除
            layer.confirm('确定删除？',{icon:3, title:'提示信息'},function(index){
                $.get("http://127.0.0.1:88/index.php/font/Font/fontDel",{
                    id : data.renwu_id  //将需要删除的newsId作为参数传入
                },function(data){
                	if(data.code==200){
                		layer.msg(data.msg);
                		setTimeout(function(){
                			tableIns.reload();
                    		layer.close(index);
                		},1000);
                	}else{
                		layer.msg(data.msg);
                	}
                })
            });
        }
		else if(layEvent === 'dingdanjietu'){ //删除
            var img_url = "";
			var anniu=[ '确认','取消'];
			var kaichuang=layer.open({
				type: 1,
				closeBtn: 0,
				content: '<fieldset class="layui-elem-field site-demo-button">'+'<legend><big><b id="information">订单订单截图</b></big></legend>'+
				'<form class="layui-form layui-form-pane" action="">'+
				'<div id="wangyin" class="layui-fluid layui-form layui-form-pane">'+
					'<div class="layui-form-item">'+
					'<button type="button" class="layui-btn" id="test1">上传图片</button>'+
					'<div class="layui-upload-list">'+
						'<img class="layui-upload-img" id="demo1"'+
						'<p id="demoText"></p>'+
					'</div>'+
					'</div>'+
				'</div></form></fieldset>',
				btn:anniu,
				yes: function() {
					layer.close(kaichuang);
					return false;
				},
				btn2: function() {
					
					layer.close(kaichuang);
					
					return true;
				}
			}); 
            var uploadInst = upload.render({
				elem: '#test1'
				,url: 'http://127.0.0.1:88/index.php/font/Font/dingdanImg?id='+id //改成您自己的上传接口
				,before: function(obj){
				  //预读本地文件示例，不支持ie8
				  $("#demo1").hide();
				  obj.preview(function(index, file, result){
					$('#demo1').attr('src', result); //图片链接（base64）
				  });
				}
				,done: function(res){
				  //如果上传失败
				  if(res.code == 200){
					img_url = res.url;
					renwu_id = res.renwu_id;
					$('#demo1').attr('src', img_url); //图片链接（base64）
					$("#demo1").val(img_url);
					console.log(renwu_id);
				  }else{
					return layer.msg('上传失败');
				  }
				  //上传成功
				  layer.msg('上传成功');
				  layer.close(kaichuang);
					return true;
				}
				
			});
           
        }
		else if(layEvent === 'weishenhe'){ //编辑
			$.ajax({ //结束时间
				type: 'POST',
				async:false,
				url: "http://127.0.0.1:88/index.php/font/Font/getshangjiashenhe",
				dataType: "json",
				data: "renwu_id="+id,
				success: function(sss) {
					var kaichuang=layer.open({
						type: 1,
						closeBtn: 1,
						skin: 'demo-class',
						title: '商家审核',
						area: ['500px', '300px'],
						content: 
							'<big>'+
							sss["check_reason"]+
							'</big>'
							,
						btn:[ '确认','取消'],
						yes: function() {
							layer.close(kaichuang);
							return true;
						},
						btn2: function() {
							
							layer.close(kaichuang);
							
							return true;
						}
					}); 
				}
			});
        }
    });
	
	table.on('tool(yiwancheng)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
		id = data.renwu_id;
		
        if(layEvent === 'chakanshenhe'){
			$.ajax({ //结束时间
				type: 'POST',
				async:false,
				url: "http://127.0.0.1:88/index.php/font/Font/getshangjiashenhe",
				dataType: "json",
				data: "renwu_id="+id,
				success: function(sss) {
					var kaichuang=layer.open({
						type: 1,
						closeBtn: 1,
						skin: 'demo-class',
						title: '商家审核',
						area: ['500px', '300px'],
						content: 
							'<big>'+
							sss["check_reason"]+
							'</big>'
							,
						btn:[ '确认','取消'],
						yes: function() {
							layer.close(kaichuang);
							return true;
						},
						btn2: function() {
							
							layer.close(kaichuang);
							
							return true;
						}
					}); 
				}
			});
		}
	});


})